package com.zxc.controller.student;

import com.zxc.controller.util.AjaxResult;
import com.zxc.model.*;
import com.zxc.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/student/online")
public class StudentOnlineExamCtontroller {
    @Autowired
    OnlineExamStudentService onlineExamStudentService;
    @Autowired
    OnlineExamService onlineExamService;
    @Autowired
    PageService pageService;
    @Autowired
    OnlineExamRecordService onlineExamRecordService;

    @RequestMapping("/examList")
    public String examList(HttpServletRequest request, Integer page, Model model) {
        int stuid = (int)request.getSession().getAttribute("stuid");

        List<OnlineExamStudent> onlineExamStudents = onlineExamStudentService.queryByStuId(stuid);
        List<OnlineExam> onlineExams = new ArrayList<>();
        for (int i = 0; i < onlineExamStudents.size(); i++) {
            OnlineExam onlineExam = onlineExamService.queryById(onlineExamStudents.get(i).getExamId());
            onlineExams.add(onlineExam);
        }
        Page paging = pageService.setList(page, 5, onlineExams.size(), onlineExams);
        model.addAttribute("paging", paging);
        return "student/online/examList";
    }

    @RequestMapping("/makeExam")
    public String makeExam(HttpServletRequest request, Integer examId, Model model) {
        int stuid = (int)request.getSession().getAttribute("stuid");
        OnlineExam onlineExam = onlineExamService.queryById(examId);
        StudentExamDetail examDetail =  onlineExamService.getOnlineExamForStudentByOnlineExamId(examId, stuid);

        model.addAttribute("onlineExam", onlineExam);
        model.addAttribute("exam", examDetail);

        return "student/online/makeExam";
    }

    /**
     * 考试记录
     */
    @PostMapping("/record")
    @ResponseBody
    public AjaxResult record(HttpServletRequest request, OnlineExamRecord record) {
        int stuid = (int)request.getSession().getAttribute("stuid");
        record.setStuId(stuid);
        int i = onlineExamRecordService.insertOrUpdateRecord(record);
        if (i == -1) {
            return AjaxResult.error(500, "数据异常，请刷新页面重试或者联系管理员");
        } else if (i == 1) {
            return AjaxResult.success("当前答案已保存");
        } else if (i == 2) {
            return AjaxResult.success("当前答案已更新");
        } else {
            return AjaxResult.success();
        }
    }

    /**
     * 结束考试
     *
     * @param examId
     * @return
     */
    @PostMapping("/finish")
    @ResponseBody
    public AjaxResult finish(HttpServletRequest request, Integer examId) {
        int stuid = (int)request.getSession().getAttribute("stuid");
        try {
            onlineExamStudentService.finishExam(examId, stuid);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return AjaxResult.success();
    }

    @GetMapping("/examDetail")
    public String detail(HttpServletRequest request, Integer examId, Model model) {
        int stuid = (int)request.getSession().getAttribute("stuid");

        model.addAttribute("exam", onlineExamService.getOnlineExamForStudentByOnlineExamId(examId, stuid));
        return "student/online/detail";
    }
}
